﻿Public Class frmAddChild
    Private Sub cmdSnap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSnap.Click
        If Not camera.IsReady Then
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Exclamation)
            Exit Sub
        End If

        pbxPic.Image = camera.TakePicture
    End Sub
    Private Sub frmAddChild_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
    Private Sub frmAddChild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = Icon.FromHandle(My.Resources.noimage.GetHicon)
        camera.Init()
    End Sub
    Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
        Dim spath As String

        Dim kid As New ChildInfo

        spath = "id\child\" & IO.Path.GetRandomFileName & "._"

        'Prevent file name collision
        While IO.File.Exists(spath)
            spath = "id\child\" & IO.Path.GetRandomFileName & "._"
        End While

        kid.FirstName = txtFName.Text
        kid.LastName = txtLName.Text
        kid.Initial = txtMI.Text
        kid.Gender = cboGender.Text
        kid.ContactInformation.Line1 = txtAddress1.Text
        kid.ContactInformation.Line2 = txtAddress2.Text
        kid.ContactInformation.City = txtCity.Text
        kid.ContactInformation.State = txtState.Text
        kid.ContactInformation.ZipCode = txtZip.Text
        kid.ContactInformation.PhoneNumber = txtPhone.Text

        Try
            kid.Age = CInt(txtAge.Text)
        Catch ex As Exception
            MsgBox("Age is invalid", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Error")
            Exit Sub
        End Try

        If pbxPic.Image Is Nothing Then
            MsgBox("Please take a picture", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Error")
            Exit Sub
        End If

        kid.Photo = pbxPic.Image

        kid.SaveFile = spath

        ChildList.Add(kid)
        extend.SaveChildren()
        extend.ReloadAll()
        Me.Close()
    End Sub
End Class